package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.Label;







//import com.jgoodies.forms.factories.DefaultComponentFactory;
import javax.swing.JFormattedTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import java.awt.Font;

import javax.swing.JEditorPane;

import controllers.ClientController;
import controllers.DBController;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.ImageIcon;

import entity.UserLoginInfo;

public class SearchSampleBytitle extends JFrame {

	static String ID;
	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	ClientController clientCon1;
	DBController Dbcont;
	String Place;
	String Time;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SearchSampleBytitle frame = new SearchSampleBytitle(ID);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the Search pic By Title frame.
	 */
	public SearchSampleBytitle(String id) {
		this.ID=id;
		setFont(new Font("Dialog", Font.PLAIN, 13));
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 629, 365);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		/**/ this.setTitle("Serch Picture By Title");
		
		JLabel lblInsretPicturePlace = new JLabel("Insret Sample Place and Time : ");
		lblInsretPicturePlace.setBounds(27, 16, 301, 25);
		lblInsretPicturePlace.setFont(new Font("Tahoma", Font.PLAIN, 20));
		
		textField = new JTextField();
		textField.setBounds(119, 69, 170, 20);
		textField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String Place =textField.getText();
				
				
			}
		});
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setBounds(119, 107, 170, 20);
		textField_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String Time =textField_1.getText();
			}
		});
		textField_1.setColumns(10);
		
		JLabel lblPlace = new JLabel("Place");
		lblPlace.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblPlace.setBounds(40, 72, 52, 14);
		
		JLabel lblTime = new JLabel("Time");
		lblTime.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblTime.setBounds(40, 110, 49, 14);
		
		JButton btnSerch = new JButton("Search"); 
		btnSerch.setBounds(96, 155, 90, 51);
			
		btnSerch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
					String Place =textField.getText();
					//JOptionPane.showMessageDialog(null, "Place is "+Place);
					btnSearchActionPerformed(e);
					
				
				}catch(IOException e1) {
					e1.printStackTrace();
				}
			
			}
		});

	

		
		JButton btnCancel = new JButton("Back");
		btnCancel.setBounds(196, 155, 93, 51);
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				PictureCustomer frame = new PictureCustomer(ID);
				frame.setVisible(true);
				dispose();
			}
		});
		contentPane.setLayout(null);
		contentPane.add(btnSerch);
		contentPane.add(btnCancel);
		contentPane.add(lblPlace);
		contentPane.add(lblTime);
		contentPane.add(textField_1);
		contentPane.add(textField);
		contentPane.add(lblInsretPicturePlace);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon("images//spiral-rainbow.jpg"));
		lblNewLabel.setBounds(0, 0, 656, 327);
		contentPane.add(lblNewLabel);
		contentPane.setLayout(null);
	}
	/**
	 * connect with the server, handleMessageFromGui
	 * @param e
	 * @throws IOException
	 */
	private void btnSearchActionPerformed(ActionEvent e) throws IOException
	{
		String Time =textField_1.getText();         //get time inserted
		String Place=textField.getText();   // get Place as inserted
		
		if(Time.equals(""))
			{
				JOptionPane.showMessageDialog(null, "No time is entered, Try again ");
			}
		else if (Place.equals("") ) 
			{
				JOptionPane.showMessageDialog(null, "No place is entered, Try again");
			}
	

			ArrayList<String> arrSearchPic=new ArrayList<String>();    
			UserLoginInfo logi=new UserLoginInfo();
			String ip=logi.getIP();
			int port=5555;
			arrSearchPic.add("SearchSamp");
			arrSearchPic.add(Place);
			arrSearchPic.add(Time);
			arrSearchPic.add(ID);
		
			this.clientCon1=new ClientController(ip,port);		
			clientCon1.handleMessageFromGui(arrSearchPic);
			
	}
	
	
}

